package webviewdemo.printfuture.com.retrofitdownloadapk;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Process;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期：2018/3/27
 * <p>
 * 描述:
 * <p>
 * Created by admin.
 * <p>
 * gitHub: https://github.com/KungFuteddy
 * <p>
 * Orporate Name: Henan Huimeng future network technology Co. Ltd.
 */

public class BaseActivity extends AppCompatActivity {

	private String TAG = "BaseActivity";

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ManifestPermission();
	}

	//需要校验的权限 读写 文件夹的
	private String[] mPermission = new String[]{
			Manifest.permission.WRITE_EXTERNAL_STORAGE,
			Manifest.permission.READ_EXTERNAL_STORAGE,
	};

	private List<String> mRequestPermission = new ArrayList<>();
	public static int PERMISSION_REQ = 2;

	//权限校验核心代码
	private void ManifestPermission() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
			for (String one : mPermission) {
				if (PackageManager.PERMISSION_GRANTED != this.checkPermission(one, Process.myPid(), Process.myUid())) {
					mRequestPermission.add(one);
				}
			}
			if (!mRequestPermission.isEmpty()) {
				this.requestPermissions(mRequestPermission.toArray(new String[mRequestPermission.size()]), PERMISSION_REQ);
				return;
			}
		}
		startActiviy();
	}


	//权限检验的提示框
	public void startActiviy() {
		if (mRequestPermission.isEmpty()) {
			final ProgressDialog mProgressDialog = new ProgressDialog(this);
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			mProgressDialog.setTitle("权限管理");
			mProgressDialog.setCancelable(false);
			mProgressDialog.show();
			new Thread(new Runnable() {
				@Override
				public void run() {
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							mProgressDialog.cancel();
							Log.i(TAG, "startActivity: ");
						}
					});
				}
			}).start();
		} else {
			Toast.makeText(this, "请打开您的权限", Toast.LENGTH_LONG).show();
		}
	}

}
